package server;

import com.myservlet.HttpServlet;

import java.util.List;

public class Service {
    public int port;
    public List<Mapper.Host> hosts;

    public HttpServlet map(HttpRequest request, HttpResponse response) {
        if(request.getUrl().equals("/favicon.ico")) {
            return null;
        }
        String substring = request.getUrl().substring(1);
        String context = substring.substring(0, substring.indexOf("/"));
        String servletUrl = substring.substring(substring.indexOf("/"));
        for (Mapper.Host host : this.hosts) {
            for (Mapper.Context context1 : host.contexts) {
                if(context1.name.equals(context)) {
                    response.setAppBase(host.appBase);
                    for (Mapper.Wrapper wrapper : context1.wrappers) {
                        if(wrapper.name.equals(servletUrl)) {
                            return (HttpServlet)wrapper.object;
                        }
                    }
                }
            }
        }
        return null;
    }
}
